package com.borrower.action;

import com.borrower.service.BorrowerService;
import com.borrower.service.BorrowerServiceImpl;
import com.pojo.BorrowerBean;
import com.pojo.UserSendMsgBean;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 借阅者发送留言模块
 */
public class BorrowerMsg extends Action{

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out=response.getWriter();
        String msg=request.getParameter("msg");
        System.out.println(msg);
        //得到当前登录人的id

        HttpSession session=request.getSession();
        String LoginName= (String) session.getAttribute("loginName");
        if(LoginName==null){
            response.sendRedirect("error.html");
        }
        System.out.println(LoginName);




        BorrowerService service = new BorrowerServiceImpl();
        List<BorrowerBean> list=service.UserInfoService(LoginName);
        Long userId= null;
        for(BorrowerBean be:list){
            userId=be.getUserId();
        }
        System.out.println(userId);

        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");

        UserSendMsgBean sendMsgBean = new UserSendMsgBean();
        sendMsgBean.setMsgSendTime(dateFormat.format(date));
        sendMsgBean.setMsg(msg);
        sendMsgBean.setUserId(userId);
        System.out.println("时间"+dateFormat.format(date));

        if(service.SendMsg(sendMsgBean)){
            out.print("留言成功");
        }else {
            out.print("留言失败");
        }






    }
}
